package com.ts.listener;

import com.qf.constatns.RabbitMQConstatns;
import com.qf.vo.Goods;
import com.rabbitmq.client.Channel;
import com.ts.service.ISearchGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class GoodsESQueueListener {

    @Autowired
    private ISearchGoodsService goodsService;

    @RabbitListener(queues = RabbitMQConstatns.GOODS_ES_QUEUE)
    public void addGoodsToEs(Goods goods, Channel channel, Message message) throws IOException {
        try {
            // 插入数据到es
            goodsService.addGoods(goods);
            // 手动ack
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            log.error("添加es出现了异常", e);
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
        }
    }
}
